package org.springblade.modules.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.platform.dto.NoticeTableListByPageDTO;
import org.springblade.modules.platform.dto.NoticeTableResionDTO;
import org.springblade.modules.platform.entity.NoticAttributeTable;
import org.springblade.modules.platform.entity.NoticeTable;
import org.springblade.modules.platform.mapper.NoticAttributeTableMapper;
import org.springblade.modules.platform.mapper.NoticeTableMapper;
import org.springblade.modules.platform.service.INoticeTableService;
;
import org.springblade.modules.platform.utils.TimeUtils;
import org.springblade.modules.platform.vo.NoticeTableInfoVO;
import org.springblade.modules.platform.vo.NoticeTableListByPageVO;
import org.springblade.modules.platform.vo.enums.NoticeTableStartEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SYG
 * @since 2022-08-25
 */
@Service
public class NoticeTableServiceImpl extends ServiceImpl<NoticeTableMapper, NoticeTable> implements INoticeTableService {

    @Autowired
    NoticAttributeTableMapper noticAttributeTableMapper;


    @Override
    public NoticeTableListByPageVO getListByPage(NoticeTableListByPageDTO noticeTableListByPageDTO, List<String> collect) {
        NoticeTableListByPageVO noticeTableListByPageVO = new NoticeTableListByPageVO();
        QueryWrapper<NoticeTable> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(noticeTableListByPageDTO.getEquipmentName())){
            queryWrapper.lambda().like(NoticeTable::getEquipmentName,noticeTableListByPageDTO.getEquipmentName());
        }
        if(noticeTableListByPageDTO.getEndTime()!=null&&noticeTableListByPageDTO.getStartTime()!=null){
            Date date = TimeUtils.currentMax(new Date(noticeTableListByPageDTO.getEndTime()));
            Date date1 = TimeUtils.currentMin(new Date(noticeTableListByPageDTO.getStartTime()));
            queryWrapper.lambda().between(NoticeTable::getGatherTime,date1.getTime(),date.getTime());

        }
		queryWrapper.lambda().orderByDesc(NoticeTable::getGatherTime);
        Page<NoticeTable> rawDataTablePage = baseMapper.selectPage(new Page<>(noticeTableListByPageDTO.getCurrent(),noticeTableListByPageDTO.getSize()), queryWrapper);
        noticeTableListByPageVO.setTotal(rawDataTablePage.getTotal());
        if(rawDataTablePage.getTotal()>0){
            List<NoticeTable> records = rawDataTablePage.getRecords();
            List<NoticeTableListByPageVO.NoticeTableVO> collect1 = records.stream().map(x -> {
                NoticeTableListByPageVO.NoticeTableVO physicalDataTableVO = new NoticeTableListByPageVO.NoticeTableVO();
                BeanUtils.copyProperties(x, physicalDataTableVO);
                physicalDataTableVO.setNoticeTableStartEnum(NoticeTableStartEnum.getEnum(x.getState()));
                return physicalDataTableVO;
            }).collect(Collectors.toList());
            noticeTableListByPageVO.setList(collect1);
        }
        return noticeTableListByPageVO;
    }

    @Override
    public void updateReason(NoticeTableResionDTO noticeTableResionDTO) {
        List<NoticeTable> noticeTables = baseMapper.selectBatchIds(noticeTableResionDTO.getIds());
        if(!CollectionUtils.isEmpty(noticeTables)){
            noticeTables.forEach(x->{
                x.setReason(noticeTableResionDTO.getReason());
                x.setState(NoticeTableStartEnum.DEALWITH.getCode());
                baseMapper.updateById(x);
            });
        }
    }

    @Override
    public NoticeTableInfoVO getInfo(Integer id) {
        NoticeTableInfoVO noticeTableInfoVO = new NoticeTableInfoVO();
        NoticeTable noticeTable = baseMapper.selectById(id);
        if(noticeTable != null){
            BeanUtils.copyProperties(noticeTable,noticeTableInfoVO);
            noticeTableInfoVO.setNoticeTableStartEnum(NoticeTableStartEnum.getEnum(noticeTable.getState()));
            List<NoticAttributeTable> noticAttributeTables = noticAttributeTableMapper.selectList(Wrappers.<NoticAttributeTable>query().lambda().eq(NoticAttributeTable::getNoticeTableId, id));
            if(!CollectionUtils.isEmpty(noticAttributeTables)){
                List<NoticeTableInfoVO.NoticAttributeTableVO> collect = noticAttributeTables.stream().map(x -> {
                    NoticeTableInfoVO.NoticAttributeTableVO noticAttributeTableVO = new NoticeTableInfoVO.NoticAttributeTableVO();
                    BeanUtils.copyProperties(x, noticAttributeTableVO);
                    return noticAttributeTableVO;
                }).collect(Collectors.toList());
                noticeTableInfoVO.setList(collect);
            }
        }
        return noticeTableInfoVO;
    }
}
